<?php
namespace app\api\controller\v1;
use think\admin\Controller;
use think\Response;
use app\data\model\DataBreed;
use app\data\model\DataGender;
use app\data\model\DataCat;
use app\data\model\DataCatCase;
use app\data\model\DataCatRemarks;
use app\data\model\DataCatToiletLog;
use app\data\model\DataDeviceLog;

/**
 * 宠物数据接口
 * @class Pet
 * @package app\api\controller\v1
 *
 */
class Pet extends Controller
{
    //宠物详情
    function get(){
        $id = input('get.id/d');
        $res = DataCat::mk()->alias('c')->field('c.*,b.breed_name,g.gender_name')
            ->join('data_breed b','b.id = c.breed')
            ->join('data_gender g','g.id = c.gender')
            ->filter(function($item) {
                $item['state'] = 0;
                return $item;
            })
            ->where(['c.id'=>$id,'c.is_deleted'=>0])->find();
        $this->success('获取成功',$res);
    }
    function del(){
        $id = input('post.id');
        $res = DataCat::mk()->where(['id'=>$id])->update(['is_deleted'=>1]);
        $this->success('删除成功',$res);
    }
    //我的宠物
    public function my()
    {

        $member_id = $this->request->uid;
        $map[]=['member_id','=',$member_id];
        $res = DataCat::mk()->getList($map);
        $this->success('获取成功',$res);
    }
    //宠物列表 分页
    public function get_list()
    {
        $member_id = $this->request->uid;
        $res = DataCat::mk()->alias('c')->field('c.*,b.breed_name,g.gender_name')
            ->join('data_breed b','b.id = c.breed')
            ->join('data_gender g','g.id = c.gender')
            ->filter(function($item) {
                $item['state'] = 0;
                return $item;
            })
            ->where(['c.member_id'=>$member_id,'c.is_deleted'=>0])->paginate(10);
        $this->success('获取成功',$res);
    }
    //宠物品种
    public function breed()
    {
        $data = DataBreed::mk()->field('id,breed_name as name,photo as avatar')
            ->where(['is_deleted'=>0])->order('sort asc,id asc')->select()->toArray();
        $res = groupByInitials($data,'name');
        $arr=[];
        $arr1=[];
        foreach($res as $k =>$v){
            $arr['letter']=$k;
            $arr['data']=$v;
            array_push($arr1,$arr);
        }
        $hot = DataBreed::mk()->field('id,breed_name as name,photo as avatar')
            ->where(['is_deleted'=>0,'is_tj'=>1])->order('sort asc,id asc')->select()->toArray();
        $res['data']=$arr1;
        $res['hot']=$hot;
        $this->success('获取成功',$res);
    }
    //宠物性别
    public function gender()
    {
        $data = DataGender::mk()->field('id,gender_name as name,photo as cover,color')
            ->where(['is_deleted'=>0])->order('sort asc,id asc')->select()->toArray();

        $this->success('获取成功',$data);
    }
    //编辑宠物
    public function save(){
        $data = input('post.');
        if(isset($data['id'])){
            DataCat::mk()->update($data);
        }else{
            $data['member_id']=$this->request->uid;
            DataCat::mk()->save($data);
        }
        $this->success('保存成功');
    }
    //病例列表
    public function get_case_list(){
        $cat_id = input('get.cat_id/d');
        $res = DataCatCase::mk()
            ->where(['cat_id'=>$cat_id,'is_deleted'=>0])
            ->filter(function($item) {
                $item['photos'] = json_decode($item['photos'],true);
                return $item;
            })
            ->paginate(10);
        $this->success('获取成功',$res);
    }
    //保存病例
    public function save_case(){
        $data = input('post.');
        if(isset($data['id'])){
            unset($data['cat_id']);
            $data['photos'] = json_encode($data['photos']);
            DataCatCase::mk()->update($data);
        }else{
            $data['member_id']=$this->request->uid;
            $data['photos'] = json_encode($data['photos']);
            DataCatCase::mk()->save($data);
        }
        $this->success('保存成功');
    }
    //病例详情
    public function get_case(){
        $id = input('get.id/d');
        $res = DataCatCase::mk()
            ->filter(function($item) {
                $item['photos'] = json_decode($item['photos'],true);
                return $item;
            })
            ->where(['id'=>$id,'is_deleted'=>0])->find();
        $this->success('获取成功',$res);
    }
    //删除病例
    public function del_case(){
        $id = input('post.id/d');
        $res = DataCatCase::mk()->where(['id'=>$id])->update(['is_deleted'=>1]);
        $this->success('获取成功',$res);
    }
    //备注列表
    public function get_remark_list(){
        $cat_id = input('get.cat_id/d');
        $res = DataCatRemarks::mk()
            ->where(['cat_id'=>$cat_id,'is_deleted'=>0])
            ->paginate(10);
        $this->success('获取成功',$res);
    }
    //保存备注
    public function save_remark(){
        $data = input('post.');
        if(isset($data['id'])){
            unset($data['cat_id']);
            DataCatRemarks::mk()->update($data);
        }else{
            $data['member_id']=$this->request->uid;
            DataCatRemarks::mk()->save($data);
        }
        $this->success('保存成功');
    }
    //备注详情
    public function get_remark(){
        $id = input('get.id/d');
        $res = DataCatRemarks::mk()
            ->where(['id'=>$id,'is_deleted'=>0])->find();
        $this->success('获取成功',$res);
    }
    //删除备注
    public function del_remark(){
        $id = input('post.id/d');
        $res = DataCatRemarks::mk()->where(['id'=>$id])->update(['is_deleted'=>1]);
        $this->success('获取成功',$res);
    }
    //如厕日志
    function get_toilet_log(){
        $type = input('get.type')?input('get.type'):'today'; //today,week,month
        $cat_id = input('get.id');
        if($type == 'today'){
            $res = DataDeviceLog::mk()->alias('l')->field('l.*,c.nickname,c.avatar,date_format(l.create_at, "%m-%d") as day,date_format(l.create_at, "%h:%m") as time')
                ->leftjoin('data_cat c','c.id = l.cat_id')
                ->where(['l.cat_id'=>$cat_id])->whereDay('l.create_at')
                ->paginate(10)->toArray();
        }else{
            $days = $type == 'week'?7:30;
            $start_time =   date("Y-m-d",strtotime("-{$days} day"))." 00:00:00";
            $end_time =   date("Y-m-d H:i:s");
            $res = DataDeviceLog::mk()->alias('l')->field('l.*,c.nickname,c.avatar,date_format(l.create_at, "%m-%d") as day,date_format(l.create_at, "%h:%m") as time')
                ->leftjoin('data_cat c','c.id = l.cat_id')
                ->where(['l.cat_id'=>$cat_id])
                ->whereTime('l.create_at', '<=', $end_time)
                ->whereTime('l.create_at', '>=', $start_time)
                ->paginate(10)->toArray();
        }
        $this->success('获取成功',$res);
    }
    function get_weight_log(){
        $type = input('get.type')?input('get.type'):'today'; //today,week,month
        $cat_id = input('get.id');
        if($type == 'today'){
            $res = DataDeviceLog::mk()->alias('l')
                ->field('l.*,c.nickname,c.avatar')
                ->leftjoin('data_cat c','c.id = l.cat_id')
                ->where(['l.cat_id'=>$cat_id])->whereDay('l.create_at')
                ->order('create_at desc')
                ->paginate(10)->toArray();
        }else{
            $days = $type == 'week'?7:30;
            $start_time =   date("Y-m-d",strtotime("-{$days} day"))." 00:00:00";
            $end_time =   date("Y-m-d H:i:s");
            $res = DataDeviceLog::mk()->alias('l')
                ->field('l.*,c.nickname,c.avatar')
                ->leftjoin('data_cat c','c.id = l.cat_id')
                ->where(['l.cat_id'=>$cat_id])
                ->whereTime('l.create_at', '<=', $end_time)
                ->whereTime('l.create_at', '>=', $start_time)
                ->order('create_at desc')
                ->paginate(10)->toArray();
        }
        $this->success('获取成功',$res);
    }
}
